package com.qianfeng.yime;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.yime.model.User;
import com.qianfeng.yime.util.CommonUtils;
import com.qianfeng.yime.util.CurrentSingleton;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Fcy on 2015/3/12.
 */

/**
 * 米粒积分
 */
public class ScoreActivity extends BaseActivity {
    private String urlScore = Constants.API_POINT_URL + "/v1/api/stu/score/sum?stuId=";
    private String urlSign = Constants.API_POINT_URL + "/v1/api/stu/score/checkin?stuId=";

    private TextView signNum;
    private TextView sign;
    private TextView scoreNum;
    private int stuId = -1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);

        setTitle("米粒积分");
        sign = (TextView) findViewById(R.id.jifen_sign);
        signNum = (TextView) findViewById(R.id.jifen_sing_num);
        scoreNum = (TextView) findViewById(R.id.jifen_num);

        sign.setOnClickListener(this);

        CurrentSingleton singleton = CurrentSingleton.getInstance();
        User user = singleton.getUser();
        if (user == null) {
            finish();
        } else { // 一定不会为null，否则无法进入当前Activity
            stuId = user.getId();
            HttpUtils httpUtils = new HttpUtils(3000);
            httpUtils.send(HttpRequest.HttpMethod.GET, urlScore + stuId, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    // TODO 解析
                    try {
                        JSONObject json = new JSONObject(responseInfo.result);
                        Log.i(">>", "ScoreActivity:" + json.toString());
                        int code = json.getInt("code");
                        if (code == 200) {
                            JSONObject stu = json.getJSONObject("data").getJSONObject("stu");
                            Log.i(">>", stu.toString());
                            // 设置签到次数
                            int checkinCount = stu.getInt("checkin");
                            signNum.setText(Integer.toString(checkinCount));
                            // 设置积分
                            int score = stu.getInt("score");
                            scoreNum.setText(Integer.toString(score));
                        } else {
                            CommonUtils.toast(ScoreActivity.this, "获取信息失败");
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    CommonUtils.toast(ScoreActivity.this, "连接服务器异常");
                }
            });

        }
    }

    public void onClickCard(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.jifen_now: // 现有积分
                intent = new Intent(this, ScoreNowActivity.class);
                break;
            case R.id.jifen_info: // 积分说明
                intent = new Intent(this, ScoreStateActivity.class);
                break;
            case R.id.jifen_activ: // 积分活动
                intent = new Intent(this, ScoreHdActivity.class);
                break;
        }
        startActivity(intent);
    }

    /**
     * 重写的方法
     *
     * @param view
     */
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.jifen_sign: // 签到
                // TODO 签到
                if (stuId != -1) {
                    HttpUtils httpUtils = new HttpUtils(3000);
                    httpUtils.send(HttpRequest.HttpMethod.GET, urlSign + stuId, new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            JSONObject json = null;
                            try {
                                json = new JSONObject(responseInfo.result);
                                int code = json.getInt("code");
                                // TODO 根据返回码，查看结果
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onFailure(HttpException e, String s) {

                        }
                    });
                }
                break;
        }

    }
}